package cn.liit.weixin.api.basic;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.liit.weixin.api.basic.config.Config;
import cn.liit.weixin.core.basic.accesstoken.AccessTokenSupport;
import cn.liit.weixin.core.basic.jsapi.JsapiTicketSupport;

import com.alibaba.fastjson.JSONObject;

/**
 * 功能 ：提供获取jsapiTicket的接口
 * url:/jsapiticket
 * 响应:{
 * 		"jsapi_ticket":"ghOmQNNtoK8Jeb6Amhb7Jwtzw_cK1eMtRnOZY9xtDzWobMz_GO6sg7yY_NRh__B8H3mogNr6s_iBlBOfeslNN11FLol3YA_deqSBlbrKDe0",
 * 		"expires_date":1444279797432,
 * 		"expires_in":7200,
 * 		"isloaded":true,
 * 		"errcode":0,
 * 		"errmsg":"ok"
 * 		}
 * @author zhangboqi
 */
@WebServlet(urlPatterns="/jsapiticket")
public class JsapiTicketServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String privatekey = request.getParameter("privatekey");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		if(privatekey != null && privatekey.equals(Config.privatekey)){
			String ticket = JSONObject.toJSONString(JsapiTicketSupport.getJsapiTicket());
			out.print(ticket);
		}else{
			out.print("{errcode:1,errmsg:'密钥不正确'}");
		}
		out.flush();
		out.close();
	}
	
}
